package pl.edu.pjwstk.kor.project4;

import pl.wcislo.sbql4j.db4o.Db4oSBQLQuery;
import com.db4o.*;
import com.db4o.ObjectContainer;

import com.db4o.foundation.*;

import com.db4o.internal.*;
import com.db4o.internal.btree.*;

import org.apache.commons.collections.CollectionUtils;

import pl.edu.pjwstk.kor.database.Database;
import pl.edu.pjwstk.kor.database.Sbql4jQuery;
import pl.edu.pjwstk.kor.model.Employee;

import pl.wcislo.sbql4j.exception.*;
import pl.wcislo.sbql4j.java.model.runtime.*;
import pl.wcislo.sbql4j.java.model.runtime.factory.*;
import pl.wcislo.sbql4j.java.utils.ArrayUtils;
import pl.wcislo.sbql4j.java.utils.OperatorUtils;
import pl.wcislo.sbql4j.java.utils.Pair;
import pl.wcislo.sbql4j.lang.codegen.nostacks.*;
import pl.wcislo.sbql4j.lang.codegen.simple.*;




import pl.wcislo.sbql4j.lang.parser.expression.*;
import pl.wcislo.sbql4j.lang.parser.expression.OrderByParamExpression.SortType;
import pl.wcislo.sbql4j.lang.parser.terminals.*;
import pl.wcislo.sbql4j.lang.parser.terminals.operators.*;
import pl.wcislo.sbql4j.lang.types.*;
import pl.wcislo.sbql4j.lang.xml.*;
import pl.wcislo.sbql4j.model.*;
import pl.wcislo.sbql4j.model.collections.*;
import pl.wcislo.sbql4j.util.*;
import pl.wcislo.sbql4j.util.Utils;
import pl.wcislo.sbql4j.xml.model.*;
import pl.wcislo.sbql4j.xml.parser.store.*;

import java.util.*;
import java.util.Collection;


public class Project4Query_SbqlQuery0Db4o0 implements Db4oSBQLQuery {
    public Project4Query_SbqlQuery0Db4o0() {
    }

    /**
     * query='db.(Employee as emp order by (emp.getLastName()))'
    '
     **/
    public java.util.List<pl.edu.pjwstk.kor.model.Employee> executeQuery(
        final ObjectContainerBase ocb, final Transaction t) {
        final LocalTransaction transLocal = (LocalTransaction) t;
        final java.util.Collection<pl.edu.pjwstk.kor.model.Employee> _ident_Employee =
            new java.util.ArrayList<pl.edu.pjwstk.kor.model.Employee>();
        ClassMetadata _classMeta0 = ocb.classCollection()
                                       .getClassMetadata("pl.edu.pjwstk.kor.model.Employee");
        long[] _ids0 = _classMeta0.getIDs(transLocal);

        for (long _id0 : _ids0) {
            LazyObjectReference _ref0 = transLocal.lazyReferenceFor((int) _id0);
            _ident_Employee.add((pl.edu.pjwstk.kor.model.Employee) _ref0.getObject());
        }

        java.util.Collection<pl.edu.pjwstk.kor.model.Employee> _asResult_emp = _ident_Employee;
        java.util.List<pl.edu.pjwstk.kor.model.Employee> _orderByResult = new java.util.ArrayList<pl.edu.pjwstk.kor.model.Employee>();

        if (_asResult_emp != null) {
            ocb.activate(_asResult_emp, 2);
        }

        _orderByResult.addAll(_asResult_emp);

        Comparator<pl.edu.pjwstk.kor.model.Employee> _comparator0 = new Comparator<pl.edu.pjwstk.kor.model.Employee>() {
                public int compare(pl.edu.pjwstk.kor.model.Employee _leftObj,
                    pl.edu.pjwstk.kor.model.Employee _rightObj) {
                    if (_leftObj == null) {
                        return -1;
                    }

                    if (_leftObj != null) {
                        ocb.activate(_leftObj, 1);
                    }

                    if (_rightObj != null) {
                        ocb.activate(_rightObj, 1);
                    }

                    int res = 0;
                    java.lang.String _leftParam0;

                    {
                        pl.edu.pjwstk.kor.model.Employee _ident_emp = _leftObj;

                        if (_ident_emp != null) {
                            ocb.activate(_ident_emp, 1);
                        }

                        pl.edu.pjwstk.kor.model.Employee _dotEl = _ident_emp;

                        if (_ident_emp != null) {
                            ocb.activate(_ident_emp, 2);
                        }

                        java.lang.String _mth_getLastNameResult = _dotEl.getLastName();

                        if (_mth_getLastNameResult != null) {
                            ocb.activate(_mth_getLastNameResult, 1);
                        }

                        _leftParam0 = _mth_getLastNameResult;
                    }

                    java.lang.String _rightParam0;

                    {
                        pl.edu.pjwstk.kor.model.Employee _ident_emp = _rightObj;

                        if (_ident_emp != null) {
                            ocb.activate(_ident_emp, 1);
                        }

                        pl.edu.pjwstk.kor.model.Employee _dotEl = _ident_emp;

                        if (_ident_emp != null) {
                            ocb.activate(_ident_emp, 2);
                        }

                        java.lang.String _mth_getLastNameResult = _dotEl.getLastName();

                        if (_mth_getLastNameResult != null) {
                            ocb.activate(_mth_getLastNameResult, 1);
                        }

                        _rightParam0 = _mth_getLastNameResult;
                    }

                    if (_leftParam0 != null) {
                        res = _leftParam0.compareTo(_rightParam0);
                    } else {
                        return -1;
                    }

                    return res;
                }
            };

        Collections.sort(_orderByResult, _comparator0);
        pl.wcislo.sbql4j.db4o.utils.DerefUtils.activateResult(_orderByResult,
            ocb);

        return _orderByResult;
    }
}
